﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using DevExpress.Utils;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using XCI.Sys.Model;
using XCI.Helper;
using XCI.Windows.Helper;

namespace XCI.Sys.Helper
{
    public class ModuleHelper
    {
        private TreeList _tree;
        private Dictionary<string, int> _moduleImageIndexs = new Dictionary<string, int>();

        public ModuleHelper(TreeList tree)
        {
            _tree = tree;
        }

        /// <summary>
        /// 展开节点
        /// </summary>
        public void ExpandNode()
        {
            ForEachDataSouce<SysModule>(p =>
             {
                 var node = _tree.FindNodeByKeyID(p.Id);
                 if (node == null)
                 {
                     return;
                 }
                 node.Expanded = p.ExpandFlag.HasValue && p.ExpandFlag.Value; 
             });
        }

        /// <summary>
        /// 初始化模块图片列表
        /// </summary>
        /// <param name="imageList">图片列表</param>
        /// <param name="isLargeImage"></param>
        /// <returns></returns>
        public void InitModuleImageList(ImageCollection imageList, bool isLargeImage)
        {
            //bool isLargeImage = imageList.ImageSize.Width != 16;
            _moduleImageIndexs.Clear();
            imageList.Clear();
            imageList.BeginInit();
            imageList.Images.Add(isLargeImage
                ? XCI.Sys.Properties.Resources.window_32x32
                : XCI.Sys.Properties.Resources.window_16x16);
            int countIndex = 1;
            ForEachDataSouce<SysModule>(p =>
            {
                Image image = null;
                if (p.MenuFlag.HasValue && p.MenuFlag.Value)
                {
                    image = GetModuleImage(isLargeImage, isLargeImage ? p.WinLargeIcon : p.WinSmallIcon);
                }
                else
                {
                    image = isLargeImage
                        ? XCI.Sys.Properties.Resources.viewsetting_32x32
                        : XCI.Sys.Properties.Resources.viewsetting_16x16;
                }
                if (image != null)
                {
                    imageList.AddImage(image);
                    _moduleImageIndexs.Add(p.Id, countIndex);
                    countIndex++;
                }
            });
            imageList.EndInit();
            _tree.Refresh();
        }

        public Image GetModuleImage(bool isLargeImage, string name)
        {
            Image img = null;
            if (!string.IsNullOrEmpty(name))
            {
                img = DXImageHelper.GetImage(name);
            }
            return img;
            //return img ?? (img = isLargeImage
            //    ? XCI.Auth.Properties.Resources.viewsetting_32x32
            //    : XCI.Auth.Properties.Resources.viewsetting_16x16);
        }

        /// <summary>
        /// 获取模块图片序号
        /// </summary>
        /// <param name="node">节点</param>
        public int GetModuleImageIndex(TreeListNode node)
        {
            int index = 0;
            if (_moduleImageIndexs == null)
            {
                return index;
            }

            if (node.TreeList.GetDataRecordByNode(node) is SysModule entity && _moduleImageIndexs.ContainsKey(entity.Id))
            {
                index = _moduleImageIndexs[entity.Id];
            }
            return index;
        }

        /// <summary>
        /// 循环数据源
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="action">执行动作</param>
        public void ForEachDataSouce<T>(Action<T> action) where T : new()
        {
            if (action == null) return;
            var source = _tree.DataSource;
            var list = source as IList;
            if (list == null) return;
            foreach (T obj in list)
            {
                action(obj);
            }
        }
    }
}